package com.zhuqieli.database_learn.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName RabbitmqConfig
 * @Description TODO
 * @Author magc
 * @Date
 * @Version 1.0
 */
@Configuration
public class RabbitmqConfig {

    public static final String QUEUE_INFORM_DOWNLOAD = "queue_inform_download";
    public static final String QUEUE_INFORM_UPLOAD = "queue_inform_upload";
    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";

    //声明交换机
    @Bean(EXCHANGE_TOPICS_INFORM)
    public FanoutExchange EXCHANGE_TOPICS_INFORM() {
        //durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.fanoutExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }

    //声明QUEUE_INFORM_EMAIL队列
    @Bean(QUEUE_INFORM_DOWNLOAD)
    public Queue QUEUE_INFORM_DOWNLOAD() {
        return new Queue(QUEUE_INFORM_DOWNLOAD);
    }

    //声明QUEUE_INFORM_SMS队列
    @Bean(QUEUE_INFORM_UPLOAD)
    public Queue QUEUE_INFORM_UPLOAD() {
        return new Queue(QUEUE_INFORM_UPLOAD);
    }

    //ROUTINGKEY_EMAIL队列绑定交换机，指定routingKey
    @Bean
    public Binding BINDING_QUEUE_INFORM_DOWNLOAD(@Qualifier(QUEUE_INFORM_DOWNLOAD) Queue queue,
                                                 @Qualifier(EXCHANGE_TOPICS_INFORM) FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    //ROUTINGKEY_SMS队列绑定交换机，指定routingKey
    @Bean
    public Binding BINDING_ROUTINGKEY_UPLOAD(@Qualifier(QUEUE_INFORM_UPLOAD) Queue queue,
                                             @Qualifier(EXCHANGE_TOPICS_INFORM) FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }

}